<?php

/**
 * 前端页面加载
 * $Id: index.php 209 2010-11-19 07:36:35Z bwanglei@gmail.com $
 */

require_once './includes/common.php';
$view_count = get_view_count();
viewCount();

define('TEMPLATE_URL', TPLS_URL.$nonce_templet);	//前台模板URL
define('TEMPLATE_PATH', TPLS_PATH.$nonce_templet);	//前台模板路径

$blog_title = $blogname;
$logid = isset($_GET['article']) ? intval($_GET['article']) : '';

//日志列表
if (empty($action) && empty($logid)) {
	require_once AW_ROOT.'/includes/class.blog.php';
	require_once AW_ROOT.'/includes/class.comment.php';
	require_once AW_ROOT.'/includes/class.tag.php';

	$awBlog = new awBlog();
	$awTag = new awTag();
	$bookmark = isset($_GET['b']) ? abs(intval($_GET['b'])) : '';
	$record = isset($_GET['record']) ? intval($_GET['record']) : '';
	$tag = isset($_GET['tag']) ? addslashes(strval(trim($_GET['tag']))) : '';
	$keyword = isset($_GET['keyword']) ? addslashes(trim($_GET['keyword'])) : '';

	if ($record) {
		$blog_title = $record.' - '.$blogname;
		if (preg_match('/^([\d]{4})([\d]{2})$/', $record, $match)) {
			$days = getMonthDayNum($match[2], $match[1]);
			$record_stime = awStrtotime($record.'01');
			$record_etime = $record_stime+3600*24*$days;
		} else {
			$record_stime = awStrtotime($record);
			$record_etime = $record_stime+3600*24;
		}
		$sqlSegment = ' `type`=\'article\' AND time_date>='.$record_stime.' AND time_date<'.$record_etime;
	} elseif ($tag) {
		$blog_title = stripslashes($tag).' - '.$blogname;
		$articleIds = $awTag->get_tag_ids($tag);
		if ($articleIds === array()) {
			awMsg('不存在该标签', BLOG_URL);
		}
		$aid_str = array_to_str($articleIds);
		$sqlSegment = ' `type`=\'article\' AND id IN ('.$aid_str.')';
	} elseif ($keyword) {
		$blog_title = '搜索 '.stripslashes($keyword).' - '.$blogname;
		$keyword = str_replace('%', '\%', $keyword);
		$keyword = str_replace('__','\_',$keyword);
		$sqlSegment = ' `type`=\'article\' AND title LIKE \'%{'.$keyword.'}%\'';
		$pageurl .= BLOG_URL.'?keyword='.urlencode($keyword).'&page';
	} else {
		$sqlSegment = ' `type`=\'article\'';
	}
	$result_result = $awBlog->getLogsForHome($sqlSegment, $bookmark, $index_newlognum);
	$logs = $result_result[0];
	$pre_bookmark = $result_result[1];
	$next_bookmark = $result_result[2];
	//取得每个帖子的评论数量
	logs_add_comnum($logs);
	add_tag_articles($logs);
	//$page_url = pagination($lognum, $index_lognum, $page, $pageurl);

	include getViews('header');
	include getViews('log_list');
}

//浏览日志，页面
if (!empty($logid)) {
	require_once AW_ROOT.'/includes/class.blog.php';
	require_once AW_ROOT.'/includes/class.comment.php';
	require_once AW_ROOT.'/includes/class.tag.php';

	$awBlog = new awBlog();
	$awComment = new awComment();
	$awTag = new awTag();
	
	$log_data = $awBlog->getOneLogForHome($logid);
	add_tag_article($log_data);
	if ($log_data === false) awMsg('不存在该条目！', BLOG_URL);
	$blog_title = $log_data['title'].' - '.$blogname;
	
	//comments
	$checkimg = '验证码：<img style="border-style:dotted; border-width:1px;" src="'.BLOG_URL.'/checkcode.php" align="absmiddle" />&nbsp;<input name="imgcode" type="text" class="imput" size="5" tabindex="5" />';
	$ckname = isset($_COOKIE['commentposter']) ? htmlspecialchars(stripslashes($_COOKIE['commentposter'])) : '';
	$ckname = isset($_COOKIE['postermail']) ? $_COOKIE['postermail'] : '';
	$ckurl = isset($_COOKIE['posterurl']) ? $_COOKIE['posterurl'] : '';
	$comments = $awComment->getComments($log_data['id']);

	$curpage = CURPAGE_LOG;
	include getViews('header');
	$awBlog->updateViewCount($log_data['id']);
	$neighborLog = $awBlog->neighborLog($log_data['time_date']);
	extract($neighborLog);
	require_once getViews('echo_log');
}
//发表评论
if ($action == 'addcom') {
	require_once AW_ROOT.'/includes/class.comment.php';
	$awComment = new awComment();

	$comname = isset($_POST['nickname']) ? addslashes(trim($_POST['nickname'])) : '';
	$comment = isset($_POST['comment']) ? addslashes(trim($_POST['comment'])) : '';
	$commail = isset($_POST['commail']) ? addslashes(trim($_POST['commail'])) : '';
	$comurl = isset($_POST['comurl']) ? addslashes(trim($_POST['comurl'])) : '';
	$imgcode = isset($_POST['imgcode']) ? strtoupper(trim($_POST['imgcode'])) : '';
	$gid = isset($_POST['gid']) ? intval($_POST['gid']) : -1;

	$ret = $awComment->addComment($comname, $commail, $comurl, $comment, $imgcode, $gid);
	switch($ret) {
		case -1:
			awMsg('发表评论失败：该日志已关闭评论', 'javascript:history.back(-1);'); break;
		case -2:
			awMsg('发表评论失败：姓名不符合规范', 'javascript:history.back(-1);'); break;
		case -3:
			awMsg('发表评论失败：邮件地址不符合规范', 'javascript:history.back(-1);'); break;
		case -4:
			awMsg('发表评论失败：内容不符合规范', 'javascript:history.back(-1);'); break;
		case -5:
			awMsg('发表评论失败：验证码错误', 'javascript:history.back(-1);'); break;
		case 0:
			$CACHE->updateCache(array('sta', 'comment'));
			awMsg('评论发表成功', $_SERVER['REQUEST_URI'].'#comment', true); break;
		case 1:
			$CACHE->updateCache('sta');
			awMsg('评论发表成功，请等待管理员审核', $_SERVER['REQUEST_URI']); break;
	}
}
